package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.TbUser;
import com.guigu.qingcheng.service.UserServiceFeign;
import com.guigu.qingcheng.utils.JwtUtils;
import com.guigu.qingcheng.utils.MD5;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Autowired
    private UserServiceFeign userServiceFeign;

    /**
     * 转登录页面
     * @return
     */
    @GetMapping("/loginUI")
    public String loginUI() {
        return "login";
    }

    /**
     * 发送二维码请求
     * @return
     */
    @GetMapping("/getImg")
    @ResponseBody
    public String getImg() {
        return userServiceFeign.getWxCode();
    }

    /**
     * 处理前端的登录请求的业务
     * @param tbUser
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public R login(@RequestBody TbUser tbUser) {
        R result = userServiceFeign.loginUser(tbUser);

        return result;
    }

    /**
     * 处理登录用户信息的业务
     * @param request
     * @return
     */
    @GetMapping("/getUserInfo")
    @ResponseBody
    public R getUserInfo(HttpServletRequest request) {
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String username = JwtUtils.getMemberIdByJwtToken(request);

        R result = userServiceFeign.getUserInfo(username);
        return result;
    }


}
